
<xsl:stylesheet xmlns:t="http://www.tei-c.org/ns/1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                xmlns:europeana="http://www.europeana.eu/schemas/ese/"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                exclude-result-prefixes="t fn" version="1.0">
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>


    <xsl:template match="/">
        <europeana:metadata>
            <europeana:record>
                <dc:source>Moravská zemská knihovna v Brně - Moravian Library</dc:source>
                <xsl:call-template name="identifier"/>
                <dc:title>
                    <xsl:call-template name="trim">
                        <xsl:with-param name="text">
                            <xsl:value-of select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:head/t:title | /t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:head/t:title | //t:msItem/t:title | //t:msItem/t:rubic | //t:msItem/t:incipit "/>
                        </xsl:with-param>
                        <xsl:with-param name="maxLength">100</xsl:with-param>
                    </xsl:call-template>
                </dc:title>
                <dc:creator>
                    <xsl:value-of select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:head/t:persName[@type='author']"/>
                </dc:creator>
                <dc:type>manuscript</dc:type>
                <xsl:call-template name="publisher"/>
                <xsl:call-template name="date"/>
                <xsl:call-template name="description"/>
                <xsl:call-template name="subject"/>
                <xsl:call-template name="format"/>
                <xsl:call-template name="language"/>
                <xsl:call-template name="europeana"/>
            </europeana:record>
        </europeana:metadata>
    </xsl:template>

    <xsl:template name="identifier">
        <dc:identifier>
            <xsl:value-of select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/@xml:id/string()"/>
        </dc:identifier>
        <dc:identifier>
            <xsl:value-of select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:msIdentifier/t:idno"/>
        </dc:identifier>
    </xsl:template>

    <xsl:template name="trim">
        <xsl:param name="text"/>
        <xsl:param name="maxLength"/>
        <xsl:variable name="textNormalized" select="normalize-space($text)"/>
        <!--<xsl:variable name="maxLength">100</xsl:variable>-->
        <xsl:choose>
            <xsl:when test="string-length($textNormalized) &gt; number($maxLength)">
                <xsl:variable name="last" select="$maxLength - 3"/>
                <xsl:value-of select="substring($textNormalized,1,$last)"/>...
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="publisher">
        <xsl:for-each select="//t:docImprint">
            <dc:publisher>
                <xsl:value-of select="."/>
            </dc:publisher>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="date">
        <xsl:for-each select="//t:origDate">
            <dc:date>
                <xsl:value-of select="."/>
            </dc:date>
        </xsl:for-each>
        <!--<dc:date>
            <xsl:value-of select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:head/t:origDate"/>
        </dc:date>-->
    </xsl:template>

    <xsl:template name="description">
        <xsl:for-each select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:msContents/t:summary/t:note/t:p">
            <dc:description>
                <xsl:call-template name="trim">
                    <xsl:with-param name="text">
                        <xsl:value-of select="."/>
                    </xsl:with-param>
                    <xsl:with-param name="maxLength">300</xsl:with-param>
                </xsl:call-template>
            </dc:description>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="subject">
        <!--<dc:subject>
        TODO
        </dc:subject>-->
    </xsl:template>

    <xsl:template name="format">
        <dc:format>image/jpeg</dc:format>
    </xsl:template>
    
    <xsl:template name="language">
        <xsl:for-each select="//t:textLang/@mainLang">
            <!-- TODO: enable when mainLang contains just single language (as is specified) -->
<!--            <dc:language>
                <xsl:value-of select="."/>
            </dc:language>    -->
            <xsl:call-template name="languagesFromSpaceSeparatedValues"/>
        </xsl:for-each>
        <xsl:for-each select="//t:textLang/@otherLangs">
            <xsl:call-template name="languagesFromSpaceSeparatedValues"/>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="languagesFromSpaceSeparatedValues">
        <xsl:for-each select="fn:tokenize(., '\s+')">
            <dc:language>
                <xsl:value-of select="."/>
            </dc:language>    
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="europeana">
        <europeana:rights>http://www.europeana.eu/rights/rr-f/</europeana:rights>
        <xsl:variable name="baseUrl">
            <xsl:value-of select="//t:facsimile/@xml:base/string()"/>
        </xsl:variable>
        <xsl:variable name="sysno">
            <xsl:call-template name="sysnoFromUrl">
                <xsl:with-param name="baseUrl" select="$baseUrl"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="preview">
            <xsl:call-template name="previewUrl">
                <xsl:with-param name="baseUrl" select="$baseUrl"/>
            </xsl:call-template>
        </xsl:variable>

        <europeana:object>
            <xsl:value-of select="$preview"/>
        </europeana:object>
        <europeana:provider>Europeana Travel</europeana:provider>
        <europeana:type>TEXT</europeana:type>
        <europeana:dataProvider>
            Moravská zemská knihovna v Brně / Moravian Library
        </europeana:dataProvider>
        <europeana:isShownBy>
            <xsl:value-of select="$preview"/>
        </europeana:isShownBy>
        <xsl:variable name="k4_pid" select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:msIdentifier/t:altIdentifier[@type='other']/t:idno[@type='k4_pid']"/>
        <xsl:choose>
            <xsl:when test="$k4_pid">
                <europeana:isShownAt>
                    <xsl:call-template name="k4Link">
                        <xsl:with-param name="repUrl">
                            <xsl:value-of select="/t:TEI/t:teiHeader/t:fileDesc/t:sourceDesc/t:msDesc/t:msIdentifier/t:altIdentifier[@type='other']/t:idno[@type='k4_pid']/../t:repository"/>
                        </xsl:with-param>
                        <xsl:with-param name="pid">
                            <xsl:value-of select="$k4_pid"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </europeana:isShownAt>
            </xsl:when>
            <xsl:otherwise>
                <europeana:isShownAt>
                    <xsl:call-template name="alephLink">
                        <xsl:with-param name="sysno">
                            <xsl:value-of select="$sysno"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </europeana:isShownAt>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="sysnoFromUrl">
        <xsl:param name="baseUrl"/>
        <xsl:variable name = "tokens" select="tokenize(substring-after($baseUrl,'mzk03/'),'/')"/>

        <!--WTF - elment test se nezobrazuje!-->
    <!--<xsl:for-each select="$tokens">
       <test><xsl:value-of select="."/></test>
    </xsl:for-each>-->

    <!--WTF-->
    <!--<xsl:value-of select="$tokens"/>-->

    <!--<xsl:value-of select="concat($tokens)"/>-->

        <xsl:for-each select="$tokens">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="previewUrl">
        <xsl:param name="baseUrl"/>
        <xsl:variable name="firstPageId" select="//t:msItem/t:locus/@from | //t:msItem/t:msItem/t:locus/@from"/>
        <xsl:choose>
            <xsl:when test="$firstPageId">
                <xsl:call-template name="previewFromMsItem">
                    <xsl:with-param name="pageNumber" select="$firstPageId"/>
                    <xsl:with-param name="baseUrl" select="$baseUrl"/>
                </xsl:call-template >
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="//t:facsimile/t:surface[1]/t:graphic">
                    <xsl:call-template name="previewFromGraphic">
                        <xsl:with-param name="baseUrl" select="$baseUrl"/>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="previewFromMsItem">
        <xsl:param name="pageNumber"/>
        <xsl:param name="baseUrl"/>
        <xsl:for-each select="//t:facsimile/t:surface[@xml:id=string($pageNumber)]/t:graphic">
            <xsl:call-template name="previewFromGraphic">
                <xsl:with-param name="baseUrl" select="$baseUrl"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="previewFromGraphic">
        <xsl:param name="baseUrl"/>
        <xsl:variable name="url" select="./@url"/>
        <xsl:variable name="preficies">
           excellent /excellent Excellent priloha/excellent priloha/Excellent
        </xsl:variable>
        <xsl:for-each select="fn:tokenize(fn:normalize-space($preficies), '\s+')">
            <xsl:if test="starts-with($url, .)">
                <xsl:value-of select="$baseUrl"/>
                <xsl:value-of select="$url"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="alephLink">
        <!-- POZOR NA ODSAZENI TADY!!! -->
        <xsl:param name="sysno"/>
        http://aleph.mzk.cz/F?func=direct&amp;doc_number=<xsl:value-of select="$sysno"/>&amp;local_base=MZK03&amp;format=999
    </xsl:template>
    
    <xsl:template name="k4Link">
        <!-- POZOR NA ODSAZENI TADY!!! -->
        <xsl:param name="repUrl"/>
        <xsl:param name="pid"/>
        <xsl:value-of select="$repUrl"/>/search/handle/<xsl:value-of select="$pid"/>
    </xsl:template>

</xsl:stylesheet>